package com.zhs.demoutil.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhs.demoutil.entity.BaseCheckResult;
import com.zhs.demoutil.service.BaseCheckResultService;
import com.zhs.demoutil.service.CheckPointService;
import com.zhs.demoutil.service.CheckResultProService;
import com.zhs.demoutil.service.TmpCheckResultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@RestController
@Slf4j
public class ExcelController {


    @Resource
    private BaseCheckResultService baseCheckResultService;
    @Resource
    private TmpCheckResultService tmpCheckResultService;
    @Resource
    private CheckPointService checkPointService;
    @Resource
    private CheckResultProService checkResultProService;

    @PostMapping("add/base_data")
    public Object baseData(@RequestParam("file") MultipartFile file){
        // 导入基础数据
        baseCheckResultService.addByExcel(file);
        return null;
    }
    @PostMapping("add/base_data/local")
    public Object baseDataLocal(@RequestParam("file") String file){
        // 导入基础数据
        baseCheckResultService.addByExcel(file);
        return null;
    }
    @PostMapping("add/check_result")
    public String addCheckResult(@RequestParam("file") MultipartFile file){
        // 导入检测结果
        String zipPath = tmpCheckResultService.addByExcel(file);
        return zipPath;
    }
    @PostMapping("add/check_result/local")
    public String addCheckResultLocal(@RequestParam("file") String file){
        // 导入检测结果
        String zipPath = tmpCheckResultService.addByExcel(file);
        return zipPath;
    }

    @PostMapping("get")
    public String get(){
        // 导入检测结果
        String zipPath = tmpCheckResultService.onlyGet();
        return zipPath;
    }

    @PostMapping("add/check_point")
    public Object addCheckPoint(MultipartFile file){
        checkPointService.addByExcel(file);
        return null;
    }

    @PostMapping("add/check_point/local")
    public Object addCheckPointLocal(String file){
        checkPointService.addByExcel(file);
        return null;
    }

    @GetMapping("query/base_data")
    public Long queryBaseData(){
        return baseCheckResultService.count();
    }

    @GetMapping("/download")
    public void download(String path, HttpServletResponse response) {
        try {
            // path是指想要下载的文件的路径
            File file = new File(path);
            log.info(file.getPath());
            // 获取文件名
            String filename = file.getName();
            // 获取文件后缀名
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
            log.info("文件后缀名：" + ext);

            // 将文件写入输入流
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStream fis = new BufferedInputStream(fileInputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();

            // 清空response
            response.reset();
// 设置response的Header
            response.setCharacterEncoding("UTF-8");
//Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
//attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
// filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
// 告知浏览器文件的大小
            response.addHeader("Content-Length", "" + file.length());
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @GetMapping("encode")
    public Object encode(String src,String dest) throws IOException {
        //1.创建InputStreamReader对象,构造方法中传递字节输入流和指定的编码表名称GBK
        InputStreamReader isr = new InputStreamReader(new FileInputStream(src),"GB2312");
        //2.创建OutputStreamWriter对象,构造方法中传递字节输出流和指定的编码表名称UTF-8
        java.io.OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(dest), StandardCharsets.UTF_8);
        //3.使用InputStreamReader对象中的方法read读取文件
        int len = 0;
        while((len = isr.read())!=-1){
            //4.使用OutputStreamWriter对象中的方法write,把读取的数据写入到文件中
            osw.write(len);
        }
        //5.释放资源
        osw.close();
        isr.close();
        return null;
    }

}
